﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Npgsql;
using System.Configuration;
using System.Data;
using Telerik.Web.UI;

namespace IonosphereViewer
{
    public partial class SFilesNewPage : System.Web.UI.Page
    {
        DataSet myDS = new DataSet();
        DataSet ChartDS = new DataSet();
        NpgsqlConnection conn;
        //--------------------------------------------------------------------------
        //const string SqlFilesMain =
        //    @"SELECT id, measure_date, seans, nr1, nr2
        //      FROM headerall
        //      WHERE date_trunc('day', measure_date) = @vdate AND measure_type = 2";
        //--------------------------------------------------------------------------
        const string SqlFiles2 =
            @"SELECT  unnest(h_akf) as akf FROM datas WHERE header_id = @pheader_id LIMIT 18";
        //--------------------------------------------------------------------------
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Error += new EventHandler(_Default_Error);
            if (!IsPostBack)
            {
                RadCalendar.SelectedDate = DateTime.Now;
                //FillGrid();
                //FillDS();
            }
        }
        //--------------------------------------------------------------------------
        //private void FillGrid()
        //{
        //    try
        //    {
        //        this.conn = new NpgsqlConnection(LoginPage.constring);
        //        conn.Open();
        //        NpgsqlCommand comm = new NpgsqlCommand(SqlFilesMain, this.conn);
        //        NpgsqlParameter param = new NpgsqlParameter();
        //        param.ParameterName = "vdate";
        //        param.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Date;
        //        param.Value = RadCalendar.SelectedDate;
        //        comm.Parameters.Add(param);

        //        NpgsqlDataAdapter da = new NpgsqlDataAdapter(comm);

        //        da.Fill(myDS);
        //        DataTable dt = myDS.Tables[0];
        //        //RadGridV.DataSource = dt;
        //        //RadGridV.DataBind();
        //    }
        //    finally
        //    {
        //        conn.Close();
        //    }
        //}
        //-------------------------------------------------------------------------
        private void FillDS(int id)
        {
            try
            {
                long[] AKFarray = new long[10];
                this.conn = new NpgsqlConnection(LoginPage.constring);
                conn.Open();
                NpgsqlCommand commDS = new NpgsqlCommand(SqlFiles2, this.conn);
                commDS.Parameters.Add(new NpgsqlParameter("pheader_id", NpgsqlTypes.NpgsqlDbType.Bigint));
                //commDS.Parameters.Add(new NpgsqlParameter("ptype_id", NpgsqlTypes.NpgsqlDbType.Integer));
                commDS.Parameters["pheader_id"].Value = id;
                //commDS.Parameters["ptype_id"].Value = 1;
                NpgsqlDataAdapter daChart = new NpgsqlDataAdapter(commDS);
                //ChartDS.Tables.Add();
                //ChartDS.Tables[0].Columns.Add("akf", typeof(long[]));
                daChart.Fill(ChartDS);
                DataTable dt = ChartDS.Tables[0];
                RadChart1.DataSource = dt;
                RadChart1.DataBind();
            }
            finally
            {
                conn.Close();
            }
        }
        //--------------------------------------------------------------------------
        void _Default_Error(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Write("Page Error!<br />");
            Response.Write(string.Format("Error: <b>{0}</b>", Server.GetLastError().Message));
            Server.ClearError();
        }
        //--------------------------------------------------------------------------
        protected void RadCalendar_SelectionChanged(object sender, Telerik.Web.UI.Calendar.SelectedDatesEventArgs e)
        {
            //FillGrid();
        }
        //--------------------------------------------------------------------------
        protected void RadGridMain_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (RadGridMain.SelectedItems.Count > 0)
            {
                int id = int.Parse(RadGridMain.SelectedItems[RadGridMain.SelectedItems.Count-1].Cells[3].Text);
                FillDS(id);
            }
        }
        //--------------------------------------------------------------------------
        protected void RadGridMain_PreRender(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadGridMain.MasterTableView.Items[0].Selected = true;
                int id = int.Parse(RadGridMain.SelectedItems[0].Cells[3].Text);
                FillDS(id);
            }
        }
        //--------------------------------------------------------------------------
        protected void RadButtonExport_Click(object sender, EventArgs e)
        {
            Dbwriter dbw = new Dbwriter();
            for (int i = 0; i < RadGridMain.SelectedItems.Count; i++)
            {
                string error = dbw.ExportSFile(int.Parse(RadGridMain.SelectedItems[i].Cells[3].Text), HttpContext.Current);
                LabelError.Text = error;
            }
        }
        //--------------------------------------------------------------------------
    }
}
